package com.jintoufs.util;

/**
 * 简单的分页算法类
 * @author fuhuo.zhang
 *
 */
public class Pagination {

	/** 
     * 生成分页信息 包括第一页，上一页，下一页，最后一页等 
     *  
     * @param pageNum 
     *            当前页数 
     * @param pageCount 
     *            总页数 
     * @param recordCount 
     *            总记录数 
     * @param pageUrl 
     *            页面URL 
     * @return 
     */  
    public static String getPagination(int pageNum, int pageCount,  
            int recordCount, String pageUrl, int areaId, int dotId, int channelId) {  
  
        if(pageCount <= 1){  
            return "";  
        }  
          
        if (pageUrl == null || pageUrl.equals("")) {  
            throw new RuntimeException("pageUrl is null or blank String");  
        }  
          
        //String url = pageUrl.contains("?") ? pageUrl : pageUrl + "?";  
        String url = pageUrl;
        StringBuffer buffer = new StringBuffer();  
  
        buffer.append("第  " + pageNum + "/" + pageCount + " 页  共  "  
                + recordCount + " 条记录  ");  
        buffer.append(pageNum == 1 ? "第一页" : "<a href='" + url  
                + "1-"+areaId+"-"+""+dotId+"-"+channelId+".html'>第一页</a>");  
        buffer.append("&nbsp;&nbsp;");  ///news/list/1-2-0-4.html
        buffer.append(pageNum == 1 ? "上一页" : "<a href='" + url  +(pageNum - 1)
                + "-"+areaId+"-"+""+dotId+"-"+channelId+".html'>上一页</a>");  
        buffer.append("&nbsp;&nbsp;");  
        buffer.append(pageNum == pageCount ? "下一页" : "<a href='" + url  +(pageNum + 1)
                + "-"+areaId+"-"+""+dotId+"-"+channelId+".html'>下一页</a>");  
        buffer.append("&nbsp;&nbsp;");  
        buffer.append(pageNum == pageCount ? "最后一页" : "<a href='" + url  +pageCount
                + "-"+areaId+"-"+""+dotId+"-"+channelId+".html'>最后一页</a>");  
        buffer.append("&nbsp;&nbsp;&nbsp;&nbsp;");  
        buffer  
                .append("到<input type='text' name='goto_input' style='width:25px;font-size:12px;text-align:center' />页");  
        buffer  
                .append("<input type='button' name='goto_button' style='width:25px;font-size:12px' value='Go' />");  
  
        buffer.append("<script type='text/javascript'>");  
        buffer.append("    function helloweenvsfei_enter(){");  
  
        buffer.append("       if(event.keyCode == 13){");  
        buffer.append("           helloweenvsfei_goto();");  
        buffer.append("           return false;");  
        buffer.append("       }");  
        buffer.append("       return true;");  
        buffer.append("    }");  
  
        buffer.append("function helloweenvsfei_goto(){");  
        buffer.append("    var numText = document.getElementsByName('goto_input')[0].value;");  
        buffer.append("    var num = parseInt(numText, 10);");  
        buffer.append("    if(!num){");  
        buffer.append("        alert('页数必须是数字');");  
        buffer.append("        return;");  
        buffer.append("    }");  
  
        buffer.append("    if(num < 1 || num >" + pageCount + "){");  
        buffer.append("        alert('页数必须大于0，且小于总页数" + pageCount + "');");  
        buffer.append("        return;");  
        buffer.append("    }");  
        buffer.append("    location ='" + url  +"+num"
                + "-"+areaId+"-"+""+dotId+"-"+channelId+".html");  
        
        buffer.append("}");  
  
        buffer.append("document.getElementsByName('goto_input')[0].onkeypress = helloweenvsfei_enter;");  
        buffer.append("document.getElementsByName('goto_button')[0].onclick = helloweenvsfei_goto;");  
          
        buffer.append("</script>");  
          
        return buffer.toString();  
    }  
}
